﻿package com.nicolasprof.profgallery.texteffect {
	import flash.events.*;
	import flash.utils.*;
	import flash.text.*;

	public class TextType{
		
		private var _tf:TextField;
		private var _tFormat:TextFormat;
		private var _str:String;
		private var _steps:uint;
		private var _strLength:Number;
		private var _timer:Timer;

		public function TextType(tf:TextField,tfm:TextFormat) {
			_tf = tf;
			_tFormat = tfm;
			_timer = new Timer(25);
			_timer.addEventListener(TimerEvent.TIMER, typing);
			
			
		}
		public function start(str:String):void{
			_tf.text = str;
			_str = str;
			_steps = 0;
			_strLength = _str.length;
			_timer.reset();
			_timer.start();
		}
		public function stop():void{
			_tf.text = "";
			_timer.stop();
		}
		private function typing(e:TimerEvent):void{
			var normalText:String;
            
			if (_steps <= _strLength){

				normalText = _str.substr(0, _steps);
				typeResult(normalText);
				_steps++;
			
			}
			else{
				_timer.stop();
			} 

		}
		
		private function typeResult(txt:String):void{
			_tf.text = txt;
			_tf.setTextFormat(_tFormat);
		}
	}
}